package com.wanhe.shoppingcart.cuiyu;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.wanhe.base.BasePop;
import com.wanhe.base.model.base.DishesType;
import com.wanhe.base.model.holder.DishTypeHolder;
import com.wanhe.shoppingcart.R;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.bean.CartShowItem;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by XieZaiWei on 2017/6/1.
 * 脆鱼下单失败弹窗
 */

public class CuiYuOrderFailPop extends BasePop implements View.OnClickListener{

	private ExpandableListView lv_fail;
	private ExpandableListView lv_success;
	private TextView tv_total_price;
	private TextView tv_order;

	private CuiYuCartAdapter mSuccessAdapter;
	private CuiYuCartAdapter mFailAdapter;

	public CuiYuOrderFailPop(FragmentActivity context, int width, int height) {
		super(context, LayoutInflater.from(context).inflate(R.layout.cui_yu_order_fail_pop, null), width, height);
		setAnimationStyle(R.style.cartPop);
		initViews();
		initEvents();
	}


	@Override
	public void initViews() {
		tv_total_price = (TextView)findViewById(R.id.tv_total_price);
		tv_order = (TextView)findViewById(R.id.tv_order);
		lv_fail = (ExpandableListView)findViewById(R.id.lv_fail);
		lv_success = (ExpandableListView)findViewById(R.id.lv_success);

		tv_order.setOnClickListener(this);
		lv_fail.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				return true;
			}
		});

		lv_success.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				return true;
			}
		});
	}

	@Override
	public void initEvents() {
		mSuccessAdapter = new CuiYuCartAdapter(mActivity, R.layout.cui_yu_cart_group,
				R.layout.cui_yu_fail_item, R.layout.cui_yu_fail_list_item, R.layout.cui_yu_fail_set_meal_item);
		lv_success.setAdapter(mSuccessAdapter);

		mFailAdapter = new CuiYuCartAdapter(mActivity, R.layout.cui_yu_cart_group,
				R.layout.cui_yu_fail_item, R.layout.cui_yu_fail_list_item, R.layout.cui_yu_fail_set_meal_item);
		lv_fail.setAdapter(mFailAdapter);
	}

	public void setData(List<ICartItem> arrSuccess, List<ICartItem> arrFail){
		ArrayList<CartShowItem> arrFailCartShowItem = new ArrayList<>();
		for (int i = 0; i < arrFail.size(); i++) {
			CartItem cartItem = (CartItem) arrFail.get(i);
			CartShowItem cartShowItem = new CartShowItem();
			DishesType dishesType = DishTypeHolder.getInstance().get(cartItem.getTypeId());
			String title = dishesType.getVCH_TypeName();
			cartShowItem.setTitle(title);
			cartShowItem.setTypeId(cartItem.getTypeId());
			cartShowItem.setTypeNO(dishesType.getCH_TypeNo());
			if (arrFailCartShowItem.contains(cartShowItem)) {
				arrFailCartShowItem.get(arrFailCartShowItem.indexOf(cartShowItem)).getArrCartItem().add(cartItem);
			} else {
				cartShowItem.getArrCartItem().add(cartItem);
				arrFailCartShowItem.add(cartShowItem);
			}
		}
		if (mFailAdapter != null) {
			mFailAdapter.setData(arrFailCartShowItem);
			for (int i = 0; i < mFailAdapter.getGroupCount(); i++) {
				lv_fail.expandGroup(i);
			}
		}



		ArrayList<CartShowItem> arrSuccessCartShowItem = new ArrayList<>();
		for (int i = 0; i < arrSuccess.size(); i++) {
			CartItem cartItem = (CartItem) arrSuccess.get(i);
			CartShowItem cartShowItem = new CartShowItem();
			DishesType dishesType = DishTypeHolder.getInstance().get(cartItem.getTypeId());
			String title = dishesType.getVCH_TypeName();
			cartShowItem.setTitle(title);
			cartShowItem.setTypeId(cartItem.getTypeId());
			cartShowItem.setTypeNO(dishesType.getCH_TypeNo());
			if (arrSuccessCartShowItem.contains(cartShowItem)) {
				arrSuccessCartShowItem.get(arrSuccessCartShowItem.indexOf(cartShowItem)).getArrCartItem().add(cartItem);
			} else {
				cartShowItem.getArrCartItem().add(cartItem);
				arrSuccessCartShowItem.add(cartShowItem);
			}
		}
		if (mSuccessAdapter != null) {
			mSuccessAdapter.setData(arrSuccessCartShowItem);
			for (int i = 0; i < mSuccessAdapter.getGroupCount(); i++) {
				lv_success.expandGroup(i);
			}
		}


	}


	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.tv_order){
			dismiss();
		}
	}

	@Override
	public void dismiss() {
//		DearPotCartManager.getInstance().clear();
//		WaiterManager.getInstance().exit();
//		//下完一单清空人数
//		TableInfoHolder.getInstance().setCustomNum(0);
//		ViewUtil.dismissLoadingDialog();
//		//        AppContext.getInstance().finishTempActivity();
//		Intent intent = new Intent(mContext, LogginActivity.class);
//		mContext.startActivity(intent);
		super.dismiss();
	}
}
